/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.stylefeng.overall.modular.config.service.impl;

import cn.stylefeng.guns.core.common.node.ZTreeNode;
import cn.stylefeng.overall.modular.config.dao.StandardAreaMapper;
import cn.stylefeng.overall.modular.config.model.StandardArea;
import cn.stylefeng.overall.modular.config.service.IStandardAreaService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 菜单服务
 *
 * @author fengshuonan
 * @date 2017-05-05 22:20
 */
@Service
public class StandardAreaServiceImpl extends ServiceImpl<StandardAreaMapper, StandardArea> implements IStandardAreaService {

    @Resource
    private StandardAreaMapper standardAreaMapper;

    @Override
    @Transactional
    public void delArea(String areaId) {

        //删除菜单
        this.standardAreaMapper.deleteById(areaId);
    }

    @Override
    @Transactional
    public void delAreaContainSubAreas(String areaId) {

        StandardArea area = standardAreaMapper.selectById(areaId);

        //删除当前菜单
        delArea(areaId);

        //删除所有子菜单
        Wrapper<StandardArea> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("parend_id", "%[" + area.getId() + "]%");
        List<StandardArea> areas = standardAreaMapper.selectList(wrapper);
        for (StandardArea temp : areas) {
            delArea(temp.getId());
        }
    }

    @Override
    public List<Map<String, Object>> selectAreas(String condition) {
        return this.baseMapper.selectAreas(condition);
    }

    @Override
    public List<ZTreeNode> areaTreeList() {
        return this.baseMapper.areaTreeList();
    }

    @Override
    public List<ZTreeNode> areaTreeListByAreaIds(List<String> areaIds) {
        return this.baseMapper.areaTreeListByAreaIds(areaIds);
    }

    @Override
    public List<StandardArea> selectAllArea() {
        return this.standardAreaMapper.selectAllArea();
    }
}
